package com.stylefeng.guns.rest.modular.film;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huangliusong.api.film.FilmServiceApi;
import com.huangliusong.api.film.VO.CatVO;
import com.huangliusong.api.film.VO.SourceVO;
import com.huangliusong.api.film.VO.YearVo;
import com.stylefeng.guns.rest.modular.VO.ResponseVO;
import com.stylefeng.guns.rest.modular.film.VO.FilmConditionVO;
import com.stylefeng.guns.rest.modular.film.VO.FilmIndexVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huangliusong
 * @since 2019/1/12
 * {@link }
 */
@RestController
@RequestMapping("/film/")
public class FilmController {

    private  static final String IMG_PER="https://github.com/huangliusong1994";

    @Reference(interfaceClass = FilmServiceApi.class)
    private FilmServiceApi filmServiceApi;
    /**
     * 接口功能聚合
     * api聚合
     * 好处 六个接口，一次请求 同一时刻节省了5次http请求
     * 同一个接口对外暴露，降低了前后端的难度和复杂度
     * @return
     * 坏处：
     * 容易出现问题，一次请求的数据过多
     */
    //获取首页信息接口
    @RequestMapping(value = "getIndex",method = RequestMethod.GET)
    public ResponseVO getIndex(){
        FilmIndexVO filmIndexVO=new FilmIndexVO();
        //获取banner信息
        filmIndexVO.setBanners(filmServiceApi.getBanner());
        //获取正在热映的电影
        filmIndexVO.setHotFilms(filmServiceApi.getHotFilms(true,8));
        //获取即将上映的电影
        filmIndexVO.setSoonFilms(filmServiceApi.getSoonFilms(true,8));
        //票房排行榜
        filmIndexVO.setBoxRanking(filmServiceApi.getBoxRanking());
        //获取受欢迎的榜单
        filmIndexVO.setExpectRanking(filmServiceApi.getExpectRange());
        //获取前一百的
        filmIndexVO.setTop100(filmServiceApi.getTop());

        return ResponseVO.success(IMG_PER,filmIndexVO);
    }



    @RequestMapping(value = "getConditionList",method = RequestMethod.GET)
    public ResponseVO getConditionList(@RequestParam(name = "catID",required = false,defaultValue = "99")String catID,
                                       @RequestParam(name = "sourceId",required = false,defaultValue = "99")String sourceId,
                                       @RequestParam(name = "yeatId",required = false,defaultValue = "99")String yeatId){

        boolean flag=false;
        FilmConditionVO filmConditionVO=new FilmConditionVO();
        List<CatVO> cats=filmServiceApi.getCats();
        List<CatVO> catResult=new ArrayList<>();
        CatVO cat=new CatVO();
        for (CatVO catVO:cats){
            if (catVO.getCatId().equals("99")){
                cat=catVO;
                continue;
            }
            if (catVO.getCatId().equals(catID)){
                flag=true;
                catVO.setActive(true);
            }
            catResult.add(catVO);
            if (!flag){
                cat.setActive(true);
                catResult.add(cat);
            }else {
                catResult.add(cat);
            }
        }


        flag=false;
        //片源
        List<SourceVO> sources=filmServiceApi.getSource();
        List<SourceVO> sourceResult=new ArrayList<>();
        SourceVO source=new SourceVO();
        for (SourceVO sourceVO:sources){
            if (sourceVO.getSourceId().equals("99")){
                sourceVO=source;
                continue;
            }
            if (sourceVO.getSourceId().equals(catID)){
                flag=true;
                sourceVO.setActive(true);
            }
            sourceResult.add(sourceVO);
            if (!flag){
                source.setActive(true);
                sourceResult.add(sourceVO);
            }else {
                sourceVO.setActive(false);
                sourceResult.add(sourceVO);
            }
        }


        flag=false;
        //片源
        List<YearVo> years=filmServiceApi.getYear();
        List<YearVo> yearResult=new ArrayList<>();
        YearVo year=new YearVo();
        for (YearVo yearVo:years){
            if (yearVo.getYearId().equals("99")){
                yearVo=year;
                continue;
            }
            if (yearVo.getYearId().equals(catID)){
                flag=true;
                yearVo.setActive(true);
            }
            yearResult.add(yearVo);
            if (!flag){
               year.setActive(false);
                yearResult.add(yearVo);
            }else {
                yearVo.setActive(false);
                yearResult.add(yearVo);
            }
        }


        filmConditionVO.setCatInfo(catResult);
        filmConditionVO.setSourceInfo(sourceResult);
        filmConditionVO.setYearInfo(yearResult);


        return ResponseVO.success(filmConditionVO);
    }
}
